<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shipping extends MY_Controller {
	
	/**
	 * @var CtyUserModel
	 */
	public $mCounty = null;

	public function __construct(){
		parent::__construct();	

	}
	
	/**
	 * 首页
	 */
	public function index(){
		
		$this->loadModel('AdmSettingModel', 'mSetting');
		$data = [
            'title'=>'配送方式'
		];
		
		$this->view('setting/shipping/home',$data, 'datatable');
	}

    /**
     * 配送方式列表
     */
    public function getList(){

        $this->loadModel('AdmShippingModel', 'mShipping', 'admin');
        $data = $this->mShipping->adminGetList();

        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $v['shipping_name'];
            $data['aaData'][$k][] = $v['shipping_desc'];

            $data['aaData'][$k][] = $this->adminStatus($v['status'], true);

            $btnConfig = array(
                array(
                    'label' => '配送区域',
                    'icon' => '<i class="fa fa-pencil"></i>',
                    'attr' => array(
                        'href' => getUrl('setting/shipping/area?id='.$v['id']),
                        'class' => 'btn btn-default btn-xs -red-stripe'
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'classes',
                        'key' => 'edit'
                    )
                ),
            );
            $data['aaData'][$k][] = adminCreateBtn($btnConfig);
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 配送区域
     */
    public function area(){


        $data = [
            'title'=>'配送区域',
            'shipping_id'=>3
        ];

        $this->view('setting/shipping/area',$data, 'datatable');
    }

    /**
     * 配送区域列表
     */
    public function getAreaList(){
        $this->loadModel('AdmSettingModel', 'mAdm', 'admin');
        $this->loadModel('AdmShippingModel', 'mShipping', 'admin');
        $data = $this->mShipping->adminGetShippingAreaList();

        foreach ($data['data'] as $k => $v){

            $data['aaData'][$k][] = $v['shipping_area_name'];
            $config = json_decode($v['config'], true);
            $data['aaData'][$k][] = $config['money'];
            $data['aaData'][$k][] = $config['add_money'];

            //获取配送区域名称
            $regionId = json_decode($v['region_config'], true);

            $regionName = $this->mAdm->getLocationNameByIds($regionId);

            $data['aaData'][$k][] = implode(',',array_column($regionName, 'name'));


            $btnConfig = array(
                array(
                    'label' => '编辑',
                    'icon' => '<i class="fa fa-pencil"></i>',
                    'attr' => array(
                        'href' => getUrl('setting/shipping/edit?shipping_id='.$v['shipping_id'].'&id='.$v['id']),
                        'class' => 'btn btn-default btn-xs -red-stripe'
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'classes',
                        'key' => 'edit'
                    )
                ),
            );
            $data['aaData'][$k][] = adminCreateBtn($btnConfig);
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 设置配送区域
     */
    public function add(){
        if ($_POST){
            $this->_save();
        }

        $this->loadModel('AdmSettingModel', 'mSet');

        $data = array(
            'province' =>$this->mSet->getLocationByPid(1),
            'is_add' => true,
            'select_area' => array(),
        );
        $this->view('setting/shipping/save',$data, 'validation,select2,uniform');
    }
    /**
     * 编辑配送区域
     */
    public function edit(){
        $this->loadModel('AdmShippingModel', 'mShipping','admin');
        $this->loadModel('AdmSettingModel', 'mAdm', 'admin');
        if ($_POST){
            $this->_save();
        }

        $tA = $this->mShipping->getInfo(get('id'));
        if ( ! $tA){
            show_404();
        }
        $config = json_decode($tA['config'], true);
        $tA['money'] = $config['money'];
        $tA['add_money'] = $config['add_money'];

        $regionId = json_decode($tA['region_config'], true);
        $regionName = $this->mAdm->getLocationNameByIds($regionId);

        $data = array(
            'province' =>$this->mAdm->getLocationByPid(1),
            'is_add' => false,
            'shipping' => $tA,
            'select_area' => $regionName,
        );
        $this->view('setting/shipping/save',$data, 'validation,select2,uniform');
    }
    /**
     * 保存
     */
    public function _save(){
        $this->loadModel('AdmShippingModel', 'mShipping','admin');

        //表单验证
        $vConf = array(
            array('shipping_area_name', '区域名称', 'required'),
            array('shipping_id', '配送方式id', 'required'),
            array('money', '首重费用', 'required|is_numeric'),
            array('add_money', '续重费用', 'required|is_numeric'),
            array('area_list[]', '区域', 'required')
        );
        $this->formDataValidation($vConf);

        $shipping_area_name = trim(post('shipping_area_name'));
        $shipping_id = trim(post('shipping_id'));
        $id = (int)post('id');

        //验证区域名称是否重复
        $isExist = $this->mShipping->shippingAreaNameIsExist($shipping_area_name, $shipping_id, $id);
        if($isExist){
            $this->ajaxReturn(0, '区域名称已经存在');
        }

        $config['money'] = post('money');
        $config['add_money'] = post('add_money');

        $list = post('area_list');

        $data = array(
            'config' => json_encode($config),
            'shipping_area_name' => $shipping_area_name,
            'shipping_id' => trim(post('shipping_id')),
            'region_config' => json_encode($list)
        );

        if(!$id){
            $data['addtime'] = time();
        }
        //保存配送区域
        if(!($this->mShipping->saveArea($data, $id))){
            $this->ajaxReturn(0, '保存失败');
        }
        $this->ajaxReturn(1, '保存成功', NULL, getUrl('setting/shipping/area?id='.$shipping_id));
    }


	/**
	 * 获取区域列表
	 */
	public function getAreaByPid(){
		
		$this->loadModel('AdmSettingModel', 'mSetting');
		$pid = (int)get('pid');
		$tA = $this->mSetting->getLocationByPid($pid);
		$data = [];
		foreach ($tA as $v){
			$data[] = [
						'id' => $v['id'],
						'name' => $v['name']
			];
		}
		
		$this->ajaxReturn(1, '', $data);
	}
}
